package com.wuzf.navigation_test.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.wuzf.navigation_test.utils.PxUtil;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class MyImageView extends AppCompatImageView {


    public MyImageView(Context context) {
        super(context);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public static void imageDetail(MyImageView view, String imageUrl) {
        imageDetail(view, imageUrl, false);
    }

    /**
     * 因为DataBinding在加载图片的时候，会有延迟，所以使用自定义组件
     */
    @BindingAdapter({"image_url", "isCircle"})
    public static void imageDetail(MyImageView view, String imageUrl, boolean isCircle) {
        RequestBuilder<Drawable> builder = Glide.with(view).load(imageUrl);
        if (isCircle) {
            builder.transform(new CircleCrop());
        }
        // 加载到view中
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null && layoutParams.height > 0 && layoutParams.width > 0) {
            builder.load(layoutParams);
        }
    }

    @BindingAdapter({"blur_url", "radius"})
    public static void setBlurImageUrl(MyImageView view, String blurUrl, int radius) {
        Glide.with(view).load(blurUrl)
                .override(radius)
                .transform(new BlurTransformation())
                .dontAnimate()
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        view.setImageDrawable(resource);
                    }
                });
    }

    public void bindData(int width, int height, int marginLeft, String imageUrl) {
        bindData(width, height, marginLeft, PxUtil.getScreenWidth(), PxUtil.getScreenHeight(), imageUrl);
    }

    public void bindData(int width, int height, int marginLeft, int maxWidth, int maxHeight, String imageUrl) {
        // 如果没有设置长或宽需要根据图片的宽高进行设置
        if (width <= 0 || height <= 0) {
            RequestBuilder<Drawable> builder = Glide.with(this).load(imageUrl);
            builder.into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    // 获取图片固有的高宽
                    int widthPx = resource.getIntrinsicWidth();
                    int heightPx = resource.getIntrinsicHeight();
                    setSize(widthPx, heightPx, marginLeft, maxWidth, maxHeight);
                    setImageDrawable(resource);
                }
            });
        } else {
            setSize(width, height, marginLeft, maxWidth, maxHeight);
            imageDetail(this, imageUrl, false);
        }
    }

    private void setSize(int width, int height, int marginLeft, int maxWidth, int maxHeight) {
        int layoutWidth, layoutHeight;
        // 设置图片的真实宽高
        if (width > height) {
            layoutWidth = maxWidth;
            layoutHeight = (int) (height / (width * 1.0 / maxWidth));
        } else {
            layoutHeight = maxHeight;
            layoutWidth = (int) (width / (height * 1.0 / maxHeight));
        }
        ViewGroup.LayoutParams params = this.getLayoutParams();
        params.width = layoutWidth;
        params.height = layoutHeight;
        // 设置marginLeft
        marginLeft = height > width ? marginLeft : 0;
        if (params instanceof FrameLayout.LayoutParams) {
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) params;
            layoutParams.setMarginStart(marginLeft);
        }
        if (params instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) params;
            layoutParams.setMarginStart(marginLeft);
        }
        this.setLayoutParams(params);
    }

}
